package com.mz.ozy.utils;

import android.content.Context;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.mz.ozy.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by administrator on 2019/4/11.
 */
public class OptionsPickerUtils {

    /**
     * 体重选择
     *
     * @param context
     * @param listener
     */
    public static void showPersonNum(Context context, final onSelectListener listener, int min, int max) {
        final List<Integer> weightListData = SportsDataUtils.getPersonNum(min, max);
        OptionsPickerView weighttOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                listener.onConfirm(weightListData.get(options1));
            }
        })
                .setSubmitText(context.getString(R.string.confirm))
                .setCancelText(context.getString(R.string.cancel))
                .setCancelColor(ContextCompat.getColor(context, R.color.c_7e7e7e))
                .setSubmitColor(ContextCompat.getColor(context, R.color.c_73b21a))
                .build();
        weighttOptions.setPicker(weightListData);
        weighttOptions.show();
    }

    /**
     * 时间选择
     *
     * @param context
     * @param listener
     * @param calToday
     */
    public static void showDate(Context context, final onDateListener listener, Calendar calToday) {
        new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String dateStr = Utils.getDate(date);
                String dateFormatStr = Utils.getDateFormat(date);
                listener.onConfirm(dateStr + "," + dateFormatStr, date);
            }
        }).setSubmitText(context.getString(R.string.confirm))
                .setCancelText(context.getString(R.string.cancel))
                .setRangDate(calToday, null)
                .setCancelColor(ContextCompat.getColor(context, R.color.c_7e7e7e))
                .setSubmitColor(ContextCompat.getColor(context, R.color.c_73b21a))
                .build()
                .show();
    }

    /**
     * 时间选择
     *
     * @param context
     * @param listener
     * @param starttime
     * @param endtime
     */
    public static void showTime(Context context, final onTimeListener listener, String starttime, String endtime) {
        final List<String> timeScopeList = SportsDataUtils.getTimeScope();
        final List<String> timeScopeW = new ArrayList<>();
        starttime = starttime.substring(0, 5);
        endtime = endtime.substring(0, 5);
        for (int i = 0; i < timeScopeList.size(); i++) {
            String timeScope = timeScopeList.get(i);
            final String[] timeArr = timeScope.split("-");
            if ("00:00".equals(endtime)) {
                if (Utils.compareTimeStr(timeArr[0], starttime)) {
                    timeScopeW.add(timeScope);
                }
            } else {
                if (!timeArr[1].equals("00:00") && Utils.compareTimeStr(timeArr[0], starttime) && Utils.compareTimeStr(endtime, timeArr[0]) && Utils.compareTimeStr(endtime, timeArr[1])) {
                    timeScopeW.add(timeScope);
                }
            }
        }
        if (timeScopeW.size() > 0) {
            OptionsPickerView weighttOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3, View v) {
                    final String timeSp = timeScopeW.get(options1);
                    String formatTimeScopeStr = Utils.formatTimeScopeStr(timeSp);
                    listener.onConfirm(timeScopeW.get(options1), formatTimeScopeStr);
                }
            })
                    .setSubmitText(context.getString(R.string.confirm))
                    .setCancelText(context.getString(R.string.cancel))
                    .setCancelColor(ContextCompat.getColor(context, R.color.c_7e7e7e))
                    .setSubmitColor(ContextCompat.getColor(context, R.color.c_73b21a))
                    .build();
            weighttOptions.setPicker(timeScopeW);
            weighttOptions.show();
        } else {
            ToastUtils.showToast("餐厅暂未营业");
        }
    }

    private onSelectListener mListener;

    public void setOnSelectListener(onSelectListener listener) {
        mListener = listener;
    }

    public interface onTimeListener {
        void onConfirm(Object object, String timeStr);
    }

    public interface onDateListener {
        void onConfirm(Object object, Date date);
    }

    public interface onSelectListener {
        void onConfirm(Object object);
    }


}
